package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.XiaoXiFinalBean;
import com.oscar.ehome.bean.pagebean.XiaoXiXiangQingBean;
import com.oscar.ehome.bean.pagebean.XueXiFinalBean;
import com.oscar.ehome.bean.pagebean.XueXiListBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.DateUtil;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.messageview.MessageDetilActivity;
import com.oscar.ehome.view.pageview.adapter.MessageFinalAdapter;
import com.oscar.ehome.view.pageview.adapter.MessageYiduAdapter;
import com.oscar.ehome.view.pageview.adapter.XiaoXiTongJiAdapter;
import com.oscar.ehome.view.pageview.adapter.XueXiWeiDuAdapter;
import com.oscar.ehome.view.pageview.adapter.XueXiYiDuAdapter;
import com.oscar.ehome.view.pageview.view.XueXiFinalView;
import com.oscar.ehome.view.teamview.TeamDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/12/18
 */

public class StudyFinalActivity extends BasesActivity<XueXiFinalView, PagePresenter> implements XueXiFinalView, XRefreshView.XRefreshViewListener {
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.message_img)
    ImageView message_img;
    @BindView(R.id.message_img_layout)
    RelativeLayout messageImgLayout;
    @BindView(R.id.message_title)
    TextView messageTitle;
    @BindView(R.id.message_refview)
    XRefreshView messageRefview;
    @BindView(R.id.yiduliebiao)
    LinearLayout yiduliebiao;
    @BindView(R.id.weiduliebiao)
    LinearLayout weiduliebiao;
    @BindView(R.id.message_listview_weidu)
    MyList message_listview_weidu;
    @BindView(R.id.message_listview_yidu)
    MyList message_listview_yidu;
    @BindView(R.id.fasongnumber)
    TextView fasongnumber;
    @BindView(R.id.weidurenshu)
    TextView weidurenshu;
    @BindView(R.id.yidurenshu)
    TextView yidurenshu;
    @BindView(R.id.message_time_text)
    TextView messageTimeText;
    @BindView(R.id.viewone)
    View viewone;
    @BindView(R.id.viewtwo)
    View viewtwo;
    @BindView(R.id.weidutupian)
    ImageView weidutupian;
    @BindView(R.id.xiangqingtiaozhuan)
    LinearLayout xiangqingtiaozhuan;
    @BindView(R.id.yidutupian)
    ImageView yidutupian;
    public static long lastRefreshTime;
    String token = "";
    XueXiListBean.xiangxin bean;
    private PagePresenter pagePresenter;
    XueXiWeiDuAdapter adapter;
    XueXiYiDuAdapter adapteryidu;
    private int page = 1;//页数
    int weiduxianshi = 0, yiduxianshi = 1;
    List<XueXiFinalBean.UnreadUserList> list_weidu = new ArrayList<>();
    List<XueXiFinalBean.ReadUserList> list_yidu = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studyfinallist);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(StudyFinalActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("学习统计");
        token = SystemConfigFactory.getInstance(StudyFinalActivity.this).getSystemConfig().getToken();
        bean = (XueXiListBean.xiangxin) getIntent().getSerializableExtra("id");
        titleImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        messageRefview.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        messageRefview.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        messageRefview.setPullLoadEnable(false);
        // 设置上次刷新的时间
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        messageRefview.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        messageRefview.setAutoRefresh(false);
    }

    public void initData() {
        showLoading();
        messageTitle.setText(bean.getTitle());

        String photo = C.qiniu + bean.getSmallImg();
        String learnType = bean.getLearnType();
        if ("video/mp4".equals(learnType)) {//视频
            GildeUtils.loadUrlImageshipin(photo, message_img, this);
        } else if ("application/pdf".equals(learnType)) {//PDF
            GildeUtils.loadUrlImagepdf(photo, message_img, this);
        } else if ("audio/mp3".equals(learnType)) {//音频
            GildeUtils.loadUrlImageyinpin(photo, message_img, this);
        }else if ("application/vnd.ms-excel".equals(learnType) || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(learnType)) {//音频
            GildeUtils.loadUrlImageexcel(photo, message_img, this);
        }

        messageTimeText.setText(bean.getCreateDate());

        if (isNetWork()) {
            if (bean != null) {
                String s = String.valueOf(page);
                pagePresenter.xuexifinal(token, "20", s, bean.getId());
            }
        } else {
        }

        weiduliebiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weiduxianshi == 0) {
                    weiduxianshi = 1;
                    message_listview_weidu.setVisibility(View.VISIBLE);
                    weidutupian.setImageResource(R.mipmap.xiajiantou);
                    if (list_weidu.size() > 0) {
                        viewone.setVisibility(View.VISIBLE);
                    }

                } else {
                    viewone.setVisibility(View.GONE);
                    message_listview_weidu.setVisibility(View.GONE);
                    weiduxianshi = 0;
                    weidutupian.setImageResource(R.mipmap.youjian);
                }
            }
        });

        yiduliebiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yiduxianshi == 0) {
                    yiduxianshi = 1;
                    message_listview_yidu.setVisibility(View.VISIBLE);
                    yidutupian.setImageResource(R.mipmap.xiajiantou);
                    if (list_yidu.size() > 0) {
                        viewtwo.setVisibility(View.VISIBLE);
                    }
                } else {
                    viewtwo.setVisibility(View.GONE);
                    message_listview_yidu.setVisibility(View.GONE);
                    yiduxianshi = 0;
                    yidutupian.setImageResource(R.mipmap.youjian);
                }
            }
        });
        xiangqingtiaozhuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String learnType = bean.getLearnType();
                if ("video/mp4".equals(learnType)) {//视频
                    Intent intent = new Intent(StudyFinalActivity.this, VideoActivity.class);
                    intent.putExtra("id", bean.getId());
                    startActivity(intent);
                } else if ("application/pdf".equals(learnType)) {//PDF
                    Intent intent = new Intent(StudyFinalActivity.this, PdfDetialActivity.class);
                    intent.putExtra("id", bean.getId());
                    startActivity(intent);
                } else if ("audio/mp3".equals(learnType)) {//音频
                    Intent intent = new Intent(StudyFinalActivity.this, FrequencyActivity.class);
                    intent.putExtra("id", bean.getId());
                    startActivity(intent);
                }else if ("application/vnd.ms-excel".equals(learnType) || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(learnType)) {

                    Intent intent = new Intent(StudyFinalActivity.this, ExcelDetialActivity.class);
                    intent.putExtra("id", bean.getId());
                    intent.putExtra("open", "false");
                    startActivity(intent);
                }


            }
        });
        message_listview_weidu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(StudyFinalActivity.this, TeamDetailActivity.class);
                intent.putExtra("id", list_weidu.get(position).getId());
                intent.putExtra("set", 2);
                startActivity(intent);
            }
        });
        message_listview_yidu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(StudyFinalActivity.this, TeamDetailActivity.class);
                intent.putExtra("id", list_yidu.get(position).getId());
                intent.putExtra("set", 2);
                startActivity(intent);
            }
        });
    }


    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    @Override
    public void getxuexifinalMessage(XueXiFinalBean result) {
        messageRefview.stopRefresh();
        if (result != null && !result.equals("")) {
            int ss = result.getReadUserList().size();
            int s = result.getUnreadUserList().size();
            int sss = s + ss;
            fasongnumber.setText("总人数：" + sss + "人");
            weidurenshu.setText(result.getUnreadUserList().size() + "");
            yidurenshu.setText(result.getReadUserList().size() + "");
            list_weidu = result.getUnreadUserList();
            adapter = new XueXiWeiDuAdapter(this, list_weidu);
            message_listview_weidu.setAdapter(adapter);
            message_listview_yidu.setVisibility(View.VISIBLE);
            list_yidu = result.getReadUserList();
            adapteryidu = new XueXiYiDuAdapter(this, list_yidu);
            message_listview_yidu.setAdapter(adapteryidu);
            if (list_yidu.size() > 0) {
                viewtwo.setVisibility(View.VISIBLE);
            }


            dismissLoading();
        } else {
            dismissLoading();
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        if (StudyFinalActivity.this != null)
            ToastUtil.show(StudyFinalActivity.this, "请求失败，请重试！");

    }

    @Override
    public void erry(String result) {
        dismissLoading();
        if (StudyFinalActivity.this != null)
            if (result != null) {
                ToastUtil.show(StudyFinalActivity.this, result + "");
            }
        if (result.contains("过期")) {
            StudyFinalActivity.this.getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(StudyFinalActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(StudyFinalActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(StudyFinalActivity.this, LoginActivity.class);
            startActivity(intent);
            StudyFinalActivity.this.finish();
        }
    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = messageRefview.getLastRefreshTime();
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        String s = String.valueOf(page);
        pagePresenter.xuexifinal(token, "20", s, bean.getId());
        showLoading();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        String s = String.valueOf(page);

        pagePresenter.xuexifinal(token, "20", s, bean.getId());
        showLoading();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
}
